package tech.gismap.admin.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tech.gismap.admin.domain.DaTableType;
import tech.gismap.admin.domain.SysDictData;
import tech.gismap.admin.mapper.DaTableTypeMapper;
import tech.gismap.admin.web.bo.SysDictDataBo;
import tech.gismap.admin.web.vo.SysDictDataVo;
import tech.gismap.admin.mapper.SysDictDataMapper;
import tech.gismap.common.core.validate.enumd.EnumPattern;
import tech.gismap.common.mybatis.core.page.PageQuery;
import tech.gismap.common.mybatis.core.page.TableDataInfo;

import java.util.ArrayList;
import java.util.List;

/**
* 数据字典
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class SysDictDataService {

    private final SysDictDataMapper baseMapper;

    private final DaTableTypeMapper typeMapper;

    public TableDataInfo<SysDictDataVo> selectPageList(SysDictDataBo bo, PageQuery pageQuery) {
        Page<SysDictDataVo> page = baseMapper.selectVoPage(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(page);
    }

    private Wrapper<SysDictData> buildQueryWrapper(SysDictDataBo bo) {
        LambdaQueryWrapper<SysDictData> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(bo.getId()!=null,SysDictData::getId,bo.getId());
        wrapper.eq(bo.getCreateBy()!=null,SysDictData::getCreateBy,bo.getCreateBy());
        wrapper.ge(bo.getBeginCreateTime()!=null,SysDictData::getCreateTime,bo.getBeginCreateTime());
        wrapper.lt(bo.getEndCreateTime()!=null,SysDictData::getCreateTime,bo.getEndCreateTime());
        wrapper.eq(bo.getUpdateBy()!=null,SysDictData::getUpdateBy,bo.getUpdateBy());
        wrapper.ge(bo.getBeginUpdateTime()!=null,SysDictData::getUpdateTime,bo.getBeginUpdateTime());
        wrapper.lt(bo.getEndUpdateTime()!=null,SysDictData::getUpdateTime,bo.getEndUpdateTime());
        wrapper.eq(bo.getCreateDept()!=null,SysDictData::getCreateDept,bo.getCreateDept());
        wrapper.orderByDesc(SysDictData::getUpdateTime);
        return wrapper;
    }

    public void init() {
        LambdaQueryWrapper<SysDictData> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByDesc(SysDictData::getBusiness);
        wrapper.orderByDesc(SysDictData::getType);
        wrapper.orderByDesc(SysDictData::getSort);
        List<SysDictData> list = baseMapper.selectList(wrapper);
        List<DaTableType> types = new ArrayList<>();
        for (SysDictData sysDictData : list) {
            DaTableType daTableType = new DaTableType();
            daTableType.setBusiness(sysDictData.getBusiness());
            daTableType.setType(sysDictData.getType());
            daTableType.setSort(sysDictData.getSort());
            daTableType.setCss(sysDictData.getCss());
            daTableType.setIsDefault(sysDictData.getIsDefault());
            daTableType.setLabel(sysDictData.getLabel());
            daTableType.setValue(sysDictData.getValue());
            types.add(daTableType);
        }
        typeMapper.insertBatch(types);
    }
}
